/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.model.newmodel;

import com.cxhd.lis.util.DateUtil;
import com.cxhd.lis.util.ToolDateTime;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author cxhd
 */
@SuppressWarnings("serial")
public class LisPatientInfo extends Model<LisPatientInfo> {
      public static final LisPatientInfo LisPatientInfoDao=new LisPatientInfo();
  
    public String  addInfo(Controller con) throws Exception{
        String reqtime=con.getPara("REQUESTTIME");
        String sjsj=con.getPara("SJSJ");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        java.util.Date udate=sdf.parse(reqtime);
        java.util.Date usjsj=sdf.parse(sjsj);
        java.sql.Timestamp stp=new java.sql.Timestamp(udate.getTime());
        java.sql.Timestamp sj=new java.sql.Timestamp(usjsj.getTime());
             System.out.println(stp);
             String sex="男".equals(con.getPara("SEX"))?"1":"2";
             String STAYHOSPITALMODE=con.getPara("STAYHOSPITALMODE");
             if("门诊".equals(STAYHOSPITALMODE)){
                 STAYHOSPITALMODE="1";
             }else if("住院".equals(STAYHOSPITALMODE)){
                 STAYHOSPITALMODE="2";
             }else{
                 STAYHOSPITALMODE="3";
             }
        set("PK",getPk())
//                .set("REQUESTER", "")//执行医生
//                .set("EXECUTETIME", "")//执行时间
                .set("RECEIVETIME", stp)//接受时间
                .set("RECEIVER", con.getPara("REQUESTER"))//接受者
                .set("STAYHOSPITALMODE", STAYHOSPITALMODE)//病人类型
                .set("PATIENTID", con.getPara("PATIENTID"))//病人ID
                .set("SECTION",con.getPara("SECTION") )//科室
                .set("DEPART_BED", con.getPara("BED_NO"))//床号
                .set("PATIENTNAME", con.getPara("PATIENTNAME"))//病人姓名
                .set("SEX", sex)//病人性别
                .set("BIRTHDAY", con.getPara("BIRTHDAY"))//生日
                .set("NL", con.getPara("AGE"))//年龄
                .set("NLDW", con.getPara("AGE_UNIT"))//年龄单位？？
                .set("DIAGNOSTIC", con.getPara("DIAGNOSTIC"))//诊断名称
                .set("EXAMINAIMCODE", con.getPara("EXAMINAIMCODE"))//检查项目代码
//                .set("EXAMINAIM", con.getPara(""))//检查项目名称
//                .set("NOTES", con.getPara(""))//正常与否？？
                .set("FEE", con.getParaToInt("FEE"))//?????
                .set("FEESTATUS", "1")//??????
                .set("SAMPLENO", con.getPara("SAMPLENO"))//样本号
                .set("RESULTSTATUS", "1")//结果状态
//                .set("CHECKOPERATOR", con.getPara(""))//审核人
//                .set("CHECKTIME", con.getPara(""))//审核时间
                .set("BRLB", con.getPara("HISLB"))//病人类别
                .set("BRXZ", con.getPara("BRXZ"))//病人性质
//                .set("PRINTTIME", con.getPara(""))//打印时间
//                .set("COMPUTERNAME", con.getPara(""))//电脑编号
                .set("SAMPLETYPE", con.getPara("SAMPLETYPE"))//样本类型
                .set("SJSJ",sj)//送检时间
                .set("SJYS", con.getPara("SJYS"))
                .save();
         return   con.getPara("PK");     
    }
    
    public void deleteInfo(Controller con)throws Exception{
        Db.use("mysql").update("DELETE from lis_patientinfo WHERE SAMPLENO='"+con.getPara("ybh")+"'");
    }
    public void updateZT(Map<String,String> map,String name)throws Exception{
        String date=map.get("date");
        String pk=map.get("pk");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        java.util.Date udate=sdf.parse(date);
        java.sql.Timestamp time=new java.sql.Timestamp(udate.getTime());
        findById(pk)
                .set("RESULTSTATUS", "2")
                .set("CHECKOPERATOR",name)
                .set("CHECKTIME",time)
                .update();
    }
    public void updateDY(Controller con)throws Exception{
        String time=con.getPara("time");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        java.util.Date date=sdf.parse(time);
         java.sql.Timestamp stp=new java.sql.Timestamp(date.getTime());
        findById(con.getPara("pk"))
                .set("PRINTTIME",stp)
                .set("RESULTSTATUS","3")
                .update();
    }
    
    
    public String getPk(){
         List<Record> list=Db.use("mysql").find("select * from lis_user limit 0,10");
        String  value = "1";
        if(list.size()>0){
        value = (Integer.parseInt(Db.use("mysql").findFirst("SELECT MAX(pk) as id from lis_user ").get("id") + "") + 1) + "";
        }
      return value;
    }
}
